package cn.iocoder.yudao.framework.mybatis.core.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * TODO
 *
 * @className: CommonMapper
 * @author: tangkh
 * @date: 2023-09-19 17:15
 */
public  class CommonMapper {
    private Log log = LogFactory.getLog(CommonMapper.class);
    private static final String INSERT = "com.baomidou.mybatisplus.mapper.SqlRunner.Insert";
    private static final String DELETE = "com.baomidou.mybatisplus.mapper.SqlRunner.Delete";
    private static final String UPDATE = "com.baomidou.mybatisplus.mapper.SqlRunner.Update";
    private static final String SELECT_LIST = "com.baomidou.mybatisplus.mapper.SqlRunner.SelectList";
    private static final String SELECT_OBJS = "com.baomidou.mybatisplus.mapper.SqlRunner.SelectObjs";
    private static final String COUNT = "com.baomidou.mybatisplus.mapper.SqlRunner.Count";
    private static final String SQLScript = "${sql}";
    private static final String SQL = "sql";
    private SqlSessionFactory factory;

    public CommonMapper(SqlSessionFactory factory) {
        this.factory = factory;
    }

    public boolean insert(String sql, Object... args) {
        return SqlHelper.retBool(this.sqlSession().insert("com.baomidou.mybatisplus.mapper.SqlRunner.Insert", this.sqlMap(sql, args)));
    }

    public boolean delete(String sql, Object... args) {
        return SqlHelper.retBool(this.sqlSession().delete("com.baomidou.mybatisplus.mapper.SqlRunner.Delete", this.sqlMap(sql, args)));
    }

    private Map<String, String> sqlMap(String sql, Object... args) {
        Map<String, String> sqlMap = new HashMap();
        sqlMap.put("sql", StringUtils.sqlArgsFill(sql, args));
        return sqlMap;
    }

    public boolean update(String sql, Object... args) {
        return SqlHelper.retBool(this.sqlSession().update("com.baomidou.mybatisplus.mapper.SqlRunner.Update", this.sqlMap(sql, args)));
    }

    public List<Map<String, Object>> selectList(String sql, Object... args) {
        return this.sqlSession().selectList("com.baomidou.mybatisplus.mapper.SqlRunner.SelectList", this.sqlMap(sql, args));
    }

    public List<Object> selectObjs(String sql, Object... args) {
        return this.sqlSession().selectList("com.baomidou.mybatisplus.mapper.SqlRunner.SelectObjs", this.sqlMap(sql, args));
    }

    public Object selectObj(String sql, Object... args) {
        return SqlHelper.getObject(this.log, this.selectObjs(sql, args));
    }

    public long selectCount(String sql, Object... args) {
        return SqlHelper.retCount((Long)this.sqlSession().selectOne("com.baomidou.mybatisplus.mapper.SqlRunner.Count", this.sqlMap(sql, args)));
    }

    public Map<String, Object> selectOne(String sql, Object... args) {
        return (Map)SqlHelper.getObject(this.log, this.selectList(sql, args));
    }

    public Page<Map<String, Object>> selectPage(Page page, String sql, Object... args) {
        if (null == page) {
            return null;
        } else {
            RowBounds rowBounds = new RowBounds((int)page.getCurrent(), (int)page.getSize());
            page.setRecords(this.sqlSession().selectList("com.baomidou.mybatisplus.mapper.SqlRunner.SelectList", this.sqlMap(sql, args), rowBounds));
            return page;
        }
    }

    private SqlSession sqlSession() {
        return this.factory.openSession(true);
    }
}
